package qingke

import (
	"crypto/tls"
	"fmt"

	"doubimeizhi.com/utility"
	"github.com/astaxie/beego/httplib"
	log "github.com/cihub/seelog"
)

//查看洗衣房列表
func WashRoomList(userId string) interface{} {
	nusb := utility.NewURLSignBuilder(qkAppkey, qkSecret, fmt.Sprintf("https://%s/league/washroom/list", qkHost))
	nusb.SetURLParam("uid", userId)
	reqURL := nusb.BuildURL()
	//reqURL := fmt.Sprintf("https://%s/league/washroom/list?appkey=271d8700b1074a9582a8cc4cdff94c5f&uid=%s", qkHost, userId)
	b := httplib.Get(reqURL)
	b.SetHost(qkHost)
	b.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	resp := make(map[string]interface{})
	if err := b.ToJSON(&resp); err != nil {
		log.Errorf("rpc-qingke-WashRoomList, service page not found, url:%d", reqURL)
		return nil
	}
	if rooms, ok := resp["data"]; ok {
		return rooms
	}
	log.Warnf("rpc-qingke-WashRoomList,resp err, code:%v, msg:%v", resp["code"], resp["msg"])
	return nil
}

//添加洗衣房
func AddWashRoom(userId string, data interface{}) (bool, string, string) {
	nusb := utility.NewURLSignBuilder(qkAppkey, qkSecret, fmt.Sprintf("https://%s/league/washroom/add", qkHost))
	nusb.SetURLParam("uid", userId)
	reqURL := nusb.BuildURL()
	//reqURL := fmt.Sprintf("https://%s/league/washroom/add?appkey=271d8700b1074a9582a8cc4cdff94c5f&uid=%s", qkHost, userId)
	b := httplib.Post(reqURL)
	b.SetHost(qkHost)
	b.JSONBody(data)
	b.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	resp := make(map[string]interface{})
	if err := b.ToJSON(&resp); err != nil {
		log.Errorf("rpc-qingke-AddWashRoom, service page not found, url:%d", reqURL)
		return false, "", "server page not found"
	}
	log.Debugf("AddWashRoom-resp:%s", resp)
	if int(resp["code"].(float64)) == 0 {
		if roomId, ok := resp["room_id"]; ok {
			return true, roomId.(string), "success"
		}
		return true, "", "success"
	}
	log.Warnf("rpc-qingke-AddWashRoom,resp err, code:%v, msg:%v", resp["code"], resp["msg"])
	return false, "", resp["msg"].(string)
}

//删除洗衣房
func DelWashRoom(data interface{}) (int, string) {
	nusb := utility.NewURLSignBuilder(qkAppkey, qkSecret, fmt.Sprintf("https://%s/league/washroom/delete", qkHost))
	reqURL := nusb.BuildURL()
	//reqURL := fmt.Sprintf("https://%s/league/washroom/delete?appkey=271d8700b1074a9582a8cc4cdff94c5f", qkHost)
	b := httplib.Post(reqURL)
	b.SetHost(qkHost)
	b.JSONBody(data)
	b.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	resp := make(map[string]interface{})
	if err := b.ToJSON(&resp); err != nil {
		log.Errorf("rpc-qingke-DelWashRoom, service page not found, url:%d", reqURL)
		return -1, "conn qk server err"
	}
	return int(resp["code"].(float64)), resp["msg"].(string)
}

//修改洗衣房
func ModifyWashRoom(data interface{}) (int, string) {
	nusb := utility.NewURLSignBuilder(qkAppkey, qkSecret, fmt.Sprintf("https://%s/league/washroom/modify", qkHost))
	reqURL := nusb.BuildURL()
	//reqURL := fmt.Sprintf("https://%s/league/washroom/modify?appkey=271d8700b1074a9582a8cc4cdff94c5f", qkHost)
	b := httplib.Post(reqURL)
	b.SetHost(qkHost)
	b.JSONBody(data)
	b.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	resp := make(map[string]interface{})
	if err := b.ToJSON(&resp); err != nil {
		log.Errorf("rpc-qingke-ModifyWashRoom, service page not found, url:%d", reqURL)
		return -1, "conn qk server err"
	}
	return int(resp["code"].(float64)), resp["msg"].(string)
}

//获取洗衣房id列表
func GetRoomIds(userId string) interface{} {
	nusb := utility.NewURLSignBuilder(qkAppkey, qkSecret, fmt.Sprintf("https://%s/league/washroom/list/roomid", qkHost))
	reqURL := nusb.BuildURL()
	//reqURL := fmt.Sprintf("https://%s/league/washroom/list/roomid?appkey=271d8700b1074a9582a8cc4cdff94c5f&uid=%s", qkHost, userId)
	b := httplib.Get(reqURL)
	b.SetHost(qkHost)
	b.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	resp := make(map[string]interface{})
	if err := b.ToJSON(&resp); err != nil {
		log.Errorf("rpc-qingke-RoomIds, service page not found, url:%d", reqURL)
		return nil
	}
	if rooms, ok := resp["data"]; ok && rooms != nil {
		return rooms
	}
	log.Warnf("rpc-qingke-RoomIds,resp err, code:%v, msg:%v", resp["code"], resp["msg"])
	return nil
}

//根据洗衣点id获取洗衣点信息
func GetWashRoomInfoByRoomId(WashRoomId string) interface{} {
	nusb := utility.NewURLSignBuilder(qkAppkey, qkSecret, fmt.Sprintf("https://%s/league/washroom/query/byroomid/batch", qkHost))
	reqURL := nusb.BuildURL()
	//reqURL := fmt.Sprintf("https://%s/league/washroom//query/byroomid/batch?appkey=271d8700b1074a9582a8cc4cdff94c5f", qkHost)
	postData := map[string]interface{}{
		"room_ids": []string{WashRoomId},
	}
	b := httplib.Post(reqURL)
	b.JSONBody(postData)
	b.SetHost(qkHost)
	b.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	resp := make(map[string]interface{})
	if err := b.ToJSON(&resp); err != nil {
		log.Errorf("rpc-qingke-RoomIGetWashRoomInfoByRoomIdds, service page not found, url:%d", reqURL)
		return nil
	}
	if roomInfos, ok := resp["data"]; ok && roomInfos != nil {
		return roomInfos
	}
	log.Warnf("rpc-qingke-RoomIGetWashRoomInfoByRoomIdds,resp err, code:%v, msg:%v", resp["code"], resp["msg"])
	return nil
}
